package cn.rengy.web.framework.wxmp.token;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.rengy.tpa.wxmp.token.AccessToken;
import cn.rengy.tpa.wxmp.token.ticket.TicketApi;
import cn.rengy.tpa.wxmp.token.ticket.TicketResponse;
import cn.rengy.tpa.wxmp.token.ticket.TicketType;

@Service
public class JsApiTicketService extends AbstractTokenService {
	 String JSAPITICKET_KEY="wx_jsapi_ticket::";
	
	
	 String JsApiTicket_LOCK_KEY="lock::wxAppJsApiTicket::";
	
	@Override
	public String getLockKey(String appid) {
		return JsApiTicket_LOCK_KEY.concat(appid);
	}

	@Override
	public String getAccessKey(String appid) {
		return JSAPITICKET_KEY.concat(appid);
	}


	@Autowired
	private AccessTokenCacheService accessTokenCacheService;
	
	
	@Override
	TokenDTO newToken(String appid) {
		AccessToken accessToken=accessTokenCacheService.getAccessToken(appid);
		TokenDTO tokenDTO=new TokenDTO();
		TicketResponse ticketResponse=TicketApi.ticket(TicketType.jsapi, accessToken);
		tokenDTO.setExpires(ticketResponse.getExpires_in());
		tokenDTO.setToken(ticketResponse.getTicket());
		return tokenDTO;
	}
	@Override
	void afterSaveToken() {
		// TODO Auto-generated method stub
		
	}
}
